const app = getApp();

Page({

  data: {
    templates: [],
    templateIndex: "",
    empty: false,
    template_id: '',
    showAddBtn: true,
    type: 1,
  },
  onLoad: function (option) {
    let type = option.type || 1; //1打水模板 2充值模板
    this.setData({
      type,
    })
    wx.setNavigationBarTitle({
      title: type == 1 ? '选择价格模板' : "选择用户自助充值模板"
    })
    this.isFranchiser = option.is_franchiser || false;
    this.isRepair = option.is_repair || false;
    if (this.isRepair) {
      this.setData({
        showAddBtn: false
      })
    }

    let _self = this;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data, "acceptDataFromOpenerPage");
      _self.setData({
        template_id: data.template_id
      })
      if (_self.isFranchiser || _self.isRepair) {
        _self.partner_id = data.partner_id;
      }
      _self.getTemplate(_self.data.template_id);
    })
  },



  //获取打水模板配置
  getTemplate(id) {
    let url = "Partner/Village/templateList";
    let template_species;
    if (this.data.type == 1) {
      template_species = 2;
    } else if (this.data.type == 2) {
      template_species = 1;
    }
    let data = {
      page: 1,
      row: 9999,
      template_species,
      is_add_global_template: true,
    };
    if (this.isFranchiser) {
      data.partner_id = this.partner_id
    }
    if (this.isRepair) {
      url = "Manage/Template/templateList";
      data.partner_id = this.partner_id
    }
    app.ajax({
      url,
      data,
      success: res => {
        if (res.data.code == 1000) {
          let source = res.data.data.data;
          if (source.length > 0) {
            let templateIndex = '';
            if (id !== "") {
              templateIndex = source.findIndex(item => item.id == id);
              if (~templateIndex) { //将选中数据排第一
                source.unshift(source.splice(templateIndex, 1)[0]);
                templateIndex = 0;
              } else {
                templateIndex = "";
              }
            }
            this.setData({
              empty: false,
              templates: source,
              templateIndex
            })
          } else {
            this.setData({
              empty: true
            })
          }
          console.log(source);
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        app.showToast("网络错误");
      }
    })
  },


  //搜索模板
  filterTemplate(e) {
    const val = e.detail.value;
    const list = this.data.templates;
    if (list.length <= 0) {
      return false;
    }
    list.filter(current => {
      if (current.template_title.includes(val)) {
        current.show = false;
      } else {
        current.show = true;
      }
    })
    this.setData({
      name: val,
      templates: list
    })
  },

  //选择模板
  chooseTemplate(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      templateIndex: index
    })

  },


  confirm() {
    let index = this.data.templateIndex;
    let template = this.data.templates[index];
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromMiddleTemplate', {
      ...template
    });
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 500)
  },

  editTemplate(e) {
    let _self = this;
    const id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    const template = this.data.templates[index];
    let type;
    if (this.data.type == 1) {
      type = 2;
    } else if (this.data.type == 2) {
      type = 1;
    }
    wx.navigateTo({
      url: `../../template/add?type=${type}&id=${id}&from=middle${this.isFranchiser?`&is_franchiser=1`:''}`,
      events: {
        editTemplatePage: function (data) {
          if (data) {
            _self.setData({
              [`templates[${index}]`]: Object.assign(_self.data.templates[index], data.data)
            })
          }
        }
      },
      success: res => {
        const temp = Object.values(template.template);
        template.template = temp;
        res.eventChannel.emit("openTemplateAdd", {
          ...template
        });
      }
    })
  },


  addTemplate() {
    let type;
    if (this.data.type == 1) {
      type = 2;
    } else if (this.data.type == 2) {
      type = 1;
    }
    wx.navigateTo({
      url: `../../template/add?type=${type}&from=middle${this.isFranchiser?`&is_franchiser=1&partner_id=${this.partner_id}`:''}`,
    })
  },

})